package sc.writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.VIntWritable;
import org.apache.hadoop.io.WritableComparable;

/**
 * Key for the output of the last MapReduce job. These data will are read by
 * the display application.
 * 
 * @author Quentin Rossé
 *
 */
public class StockSummary implements WritableComparable<StockSummary>{

	/**
	 * symbol of the stock
	 */
	private String stock;
	
	/**
	 * scores of the stock (see IntermediateData.Type for more info about the
	 * score[i] significance)
	 */
	private int[] score;
	
	private static VIntWritable vint = new VIntWritable();
	
	public StockSummary(){}
	
	public StockSummary(StockSummary ss){
		this.stock = ss.stock;
		this.score = ss.score;
	}
	
	
	public int[] getScore() {
		return score;
	}

	public void setScore(int[] score) {
		this.score = score;
	}

	public String getStock() {
		return stock;
	}
	
	public void setStock(String stock) {
		this.stock = stock;
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		stock = Text.readString(in);
		vint.readFields(in);
		score = new int[vint.get()];
		for(int i=0; i<score.length; i++){
			vint.readFields(in);
			score[i] = vint.get();
		}
	}

	@Override
	public void write(DataOutput out) throws IOException {
		Text.writeString(out, stock);
		vint.set(score.length);
		vint.write(out);
		for(int i=0; i<score.length; i++){
			vint.set(score[i]);
			vint.write(out);
		}
	}

	@Override
	public int compareTo(StockSummary ss) {
		return stock.compareTo(ss.stock);
	}

}
